package main

import (
	"encoding/json"
	"fmt"
	"net/http"
	//	"strings"
)

var (
	actionMap map[string][]string = make(map[string][]string)

//	test map[int]string :=[]
)

func init() {
	actionMap["onlinesetting"] = []string{"wire_info", "autoip,ip,ipmask,webgate,firstdns,seconddns"}
}

func main() {
	http.HandleFunc("/servers/settingData.php", setting)
	http.Handle("/", http.FileServer(http.Dir(".")))

	if err := http.ListenAndServe(":6061", nil); err != nil {
		fmt.Println(err)
	}
}

func setting(w http.ResponseWriter, r *http.Request) {
	//	w.Write()
	r.ParseForm()
	//	fmt.Println(r.PostForm)
	//action := r.Form["action"][0] //注意后面有个0
	//	fmt.Println(action)
	post := r.Form
	//	fmt.Fprint(w, send(action))
	data, _ := json.Marshal(send(post))
	w.Write(data)
}

//send 执行发送命令，2019年7月1日
func send(post map[string][]string) map[string]interface{} {
	//	$postArr=array();
	//	$arr=$this->actionMap[$action];
	//	$fields=explode(',',$arr[1]);
	//	foreach($fields as $v){
	//		$postArr[]= isset($_POST[$v]) && strlen($_POST[$v])>0?$_POST[$v]:' ';
	//	}
	//	$setting = join('|',$postArr);
	//	$data = array(
	//		'success'=>true,
	//		'method'=>'send method',
	//		'msg'=>$setting
	//	);
	//	echo json_encode($data);
	//	$this->execCmd($arr[0],$setting);

	res := make(map[string]interface{})
	res["test"] = "good"
	res["find"] = "hi"
	//	return json.Marshal(actionMap)
	//action := post["action"][0]
	//	postArr := []string{}
	//	arr := actionMap[action]
	//fields := strings.Split(arr[1], ",")
	return res
}
